package pl.szpadel.android.gadu.packets;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList;
import pl.szpadel.android.gadu.Status;

/// Contact status notification
public class NotifyReply80 extends ReceivedPacket {

	/// Status info
	public static class StatusInfo {
		public long uin;
		public Status status;
	}
	
	/// Contact statuses contained by this packet
	public ArrayList<StatusInfo> statuses;
	
	/// C-tor
	public NotifyReply80(Header hdr, InputStream stream) throws IOException {
		super(hdr, stream);
	}

	/// C-tor
	protected NotifyReply80(Header hdr, ReadableByteChannel channel) throws IOException {
			super(hdr,channel);
	}

	@Override
	protected void readFromBuffer(ByteBuffer buffer) {
		statuses = new ArrayList<StatusInfo>();
		while (buffer.remaining() > 0) {
			StatusInfo si = new StatusInfo();
			
			si.uin = (long)(buffer.getInt());
			int s = buffer.getInt();
			
			buffer.getInt(); // features, ignored
			buffer.getInt(); // remote_ip, ignored
			buffer.getShort(); // remote_port, ignored
			buffer.get(); // image size, ignored
			buffer.get(); // unknown, ignored
			buffer.getInt(); // flags, ignored
			
			String statusDescription = getString(buffer);
			
			si.status = new Status(s, statusDescription);
			
			statuses.add(si);
		}
	}
	
	// To string
	public String toString() {
		String res = "Notify80 [ ";
		for(StatusInfo si : statuses ){
			res += "\n    (" + si.uin + "," + si.status + ")";
		}
		
		return res + " ]";
	}

}
